﻿using MaiPlanServer.Dto;
using MaiPlanServer.IServices.IMasterDataServices;
using MaiPlanServer.Model;
using MaiPlanServer.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MaiPlanServer.API.Controllers.MasterData
{
    [Route("api/[controller]")]
    [ApiController]
    public class MaterialsProductClassifyController : ControllerBase
    {
        public MaterialsProductClassifyController(IMaterialsProductCServices Db)
        {
            this.Db = Db;
        }

        public IMaterialsProductCServices Db { get; }

        [HttpGet]
        public async Task<IActionResult> GetJson(int PageIndex, int PageSize, string TypeName, string State)
        {
            try
            {
                List<MD_ITEM_TYPEDto> newList = new List<MD_ITEM_TYPEDto>();

                var list = await Db.GetJsonAsync(PageIndex, PageSize, TypeName, State);
                newList = GetItem(list);

                return Ok(BaseState.CreateInstance(200, "请求成功", newList));
            }
            catch (System.Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败ex"));
            }
        }

        [NonAction]
        private List<MD_ITEM_TYPEDto> GetItem(IEnumerable<MD_ITEM_TYPEDto> list)
        {
            List<MD_ITEM_TYPEDto> TreeList = new List<MD_ITEM_TYPEDto>();
            var parent = list.Where(x => string.IsNullOrEmpty(x.PARENT_TYPE_ID.ToString()));
            var lists = parent.ToList();
            var child = list.Where(x => !string.IsNullOrEmpty(x.PARENT_TYPE_ID.ToString()));
            var list111 = child.ToList();
            foreach (var item in parent)
            {
                //判断当前父元素有没有子元素
                var curr_child = child.Where(x => x.PARENT_TYPE_ID == item.ITEM_TYPE_ID);
                if (curr_child.Any())
                {
                    GetSubItem(item, list, curr_child);
                }
                TreeList.Add(item);
            }
            if (!parent.Any())
            {
                foreach (var item in child)
                {
                    TreeList.Add(item);
                }
            }
            return TreeList;
        }
        [NonAction]
        public void GetSubItem(MD_ITEM_TYPEDto parent, IEnumerable<MD_ITEM_TYPEDto> list, IEnumerable<MD_ITEM_TYPEDto> child)
        {
            foreach (var item in child)
            {
                var curr_child = list.Where(x => x.PARENT_TYPE_ID == item.ITEM_TYPE_ID);
                if (curr_child.Any())
                {
                    GetSubItem(item, list, curr_child);
                }

                parent.Children.Add(item);
            }
        }

        [HttpPost]
        public async Task<IActionResult> Add([FromForm] MD_ITEM_TYPE model)
        {
            try
            {
                model.ITEM_TYPE_ID = Guid.NewGuid();
                model.CREATE_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                var result = await Db.AddAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "新增成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "新增失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "新增失败，服务器异常，请联系管理员"));
            }
        }


        [HttpPut]
        public async Task<IActionResult> Put([FromForm] MD_ITEM_TYPE model)
        {
            try
            {
                var result = await Db.UpdateAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "修改成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "修改失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "修改失败，服务器异常，请联系管理员"));
            }
        }

        [HttpDelete]
        public async Task<IActionResult> Del(string Id)
        {
            try
            {
                var result = await Db.DelAsync(Id);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "删除失败，服务器异常，请联系管理员"));
            }
        }




    }
}
